<?php

namespace App\Http\Controllers\Counter;

use App\Enums\AssistantStatus;
use App\Enums\OrderFrom;
use App\Enums\TradeStatus;
use App\Enums\UserType;
use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\Counter\ChangePasswordRequest;
use App\Http\Requests\Counter\LoginRequest;
use App\Http\Resources\Counter\AssistantResource;
use App\Http\Resources\Counter\AssistantTypeResource;
use App\Http\Resources\Counter\UserResource;
use App\Models\AssistantType;
use App\Models\Order;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    public function login(LoginRequest $request)
    {
        $user = User::withTelephone($request->telephone)->first();

        if (empty($user))
            return ApiResponseHelper::json([], 400, '用户不存在');

        if ($user->type != UserType::Cashier->value)
            return ApiResponseHelper::json([], 400, '非收银员员工，不允许登陆前台收银');

        if (
            Order::where('handover', false)->where('user_id', '!=', $user->id)
                ->where('store_id', $user->store_id)
                ->where('status', TradeStatus::Payed)
                ->where('from', OrderFrom::Counter)
                ->exists()
        )
            return ApiResponseHelper::json([], 400, '存在未交班的订单，请让上一个收银员交接班');

        try {
            $user->counterWork();

            $token = $user->login($request->password);

            return ApiResponseHelper::json([
                'token' => $token->plainTextToken,
            ]);
        } catch (\Exception $exception) {
            return ApiResponseHelper::json([], 400, $exception->getMessage());
        }
    }

    public function getCurrentInfo()
    {
        $user = Auth::user();
        $user->store;

        return new UserResource($user);
    }

    public function changePassword(ChangePasswordRequest $request)
    {
        $user = Auth::user();
        if (!Hash::check($request->old_password, $user->password)) {
            return ApiResponseHelper::json([], 400, '原密码错误');
        }

        $user->password = Hash::make($request->new_password);
        $user->save();

        return ApiResponseHelper::json([]);
    }

    public function getAssistantTypeList()
    {
        $types = AssistantType::oldest('sort')->get();

        return AssistantTypeResource::collection($types);
    }

    public function getAssistantList(Request $request)
    {
        $query = User::withStore($request->store_id)->withType(UserType::Assistant);

        if (!empty($request->assistant_type_id))
            $query->where('assistant_type_id', $request->assistant_type_id);

        return AssistantResource::collection($query->get());
    }

    public function getAssistantStatusCount(Request $request)
    {
        $query = User::withStore($request->store_id ?? Auth::user()->store_id)->withType(UserType::Assistant);

        $usedCount = clone $query;
        $usedCount = $usedCount->whereIn('assistant_status', AssistantStatus::getUsedStatus())->count();

        $unusedCount = $query->whereIn('assistant_status', AssistantStatus::getUnusedStatus())->count();

        return ApiResponseHelper::json([
            'used_count' => $usedCount,
            'unused_count' => $unusedCount,
        ]);
    }
}
